﻿#include "Mapper_User.h"

#include "DataBaseConnector.h"
#include "MapperTool.h"

#include <qvariant.h>
#include <qsqlquery.h>
#include <qsqlerror.h>

Mapper_User::Mapper_User()
{
  setObjectName(getClassNameFirstLow<Mapper_User>());
}
Mapper_User::~Mapper_User()
{}

bool Mapper_User::isVaildUser(QString userName, QString password, int *userId)
{
  if(MapperTool::isSqlInjection(userName)||MapperTool::isSqlInjection(password))
    {
      return false;
    }
  QString mysql="SELECT "+string_user_id+
                " FROM `"+string_dataBaseName+
                "` WHERE "+string_user_name+" ='%1' AND "+string_user_password+" ='%2';";
  mysql=mysql.arg(userName).arg(password);
  QSqlQuery query=dataBaseConnector->exec(mysql);
  if(query.lastError().type()==QSqlError::NoError&&query.size()>0)
    {
      query.next();
      *userId=query.value(string_user_id).toUInt();
      return true;
    }
  else
    {
      return false;
    }
}

DataBaseConnector* Mapper_User::getDataBaseConnector()
{
  return dataBaseConnector;
}

void Mapper_User::setDataBaseConnector(DataBaseConnector* dataBaseConnector)
{
  this->dataBaseConnector=dataBaseConnector;
  printInjection(this,dataBaseConnector);
}

void Mapper_User::objectAutoInjection(QHash<QString, QObject *> *objMap)
{
  setDataBaseConnector(getObjFromMap<DataBaseConnector>(objMap));
}
